package logic

import (
	"context"
	"encoding/json"
	"manuscript-service/models/cache"
	"manuscript-service/models/mysql"
	"time"

	"manuscript-service/servicerpc/internal/svc"
	"manuscript-service/servicerpc/servicerpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ArticleListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewArticleListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ArticleListLogic {
	return &ArticleListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 查询
func (l *ArticleListLogic) ArticleList(in *servicerpc.ArticleListRequest) (*servicerpc.ArticleListResponse, error) {
	// todo: add your logic here and delete this line

	var list []*servicerpc.ArticleList
	result, _ := cache.Rdb.Get(l.ctx, "articleList").Result()
	if result == "" {
		list, _ = mysql.ArticleList(mysql.DB, in)
		marshal, _ := json.Marshal(list)
		cache.Rdb.Set(l.ctx, "articleList", marshal, time.Minute*120)
	} else {
		json.Unmarshal([]byte(result), &list)
	}

	return &servicerpc.ArticleListResponse{
		List: list,
	}, nil
}
